public class ListNode {
     int val;
     ListNode next;
     ListNode() {}
     ListNode(int val) { this.val = val; }
     ListNode(int val, ListNode next) { this.val = val; this.next = next; }
}

class Solution {
    public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
        ListNode head = new ListNode(-1);
        ListNode cur1 = l1;
        ListNode cur2 = l2;
        ListNode cur = head;
        int tmp = 0;
        int flag = 0;
        while(cur1 != null || cur2 != null){
            tmp = 0;
            if(cur1 != null){
                tmp += cur1.val;
                cur1 = cur1.next;
            }
            if(cur2 != null){
                tmp += cur2.val;
                cur2 = cur2.next;
            }
            tmp += flag;
            flag = 0;
            if(tmp > 9){
                tmp = tmp % 10 ;
                flag = 1;
            }
            cur.next = new ListNode(tmp);
            cur = cur.next;
        }
        if(flag != 0){
            cur.next = new ListNode(flag);
        }
        return head.next;
    }
}
